import numpy as np
from common.mnist import load_mnist


def cross_entropy_error(y, t):
    if y.ndim == 1:
        # 如果 y 的空间维度是 1
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
    batch_size = y.shape[0]
    return -np.sum(t * np.log(y + 1e-7)) / batch_size


if __name__ == '__main__':
    (x_train, t_train), (x_test, t_test), = load_mnist(normalize=True, one_hot_label=True)
    print(x_train.shape)  # (60000, 784)
    train_size = x_train.shape[0]
    print(train_size)  # 60000
    batch_size = 10
    batch_mask = np.random.choice(train_size, batch_size)
    print(batch_mask)  # [ 1535 35727 12306 40398 39786 48948 17119 49724 13916 12738]
    x_batch = x_train[batch_mask]
    print(x_batch.shape)  # (10, 784)
    t_batch = t_train[batch_mask]
